package booyah.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import booyah.application.Event;
import booyah.application.Person;
import booyah.application.Position;

/**
 * 
 */
public class EventServiceImpl extends JpaDaoSupport implements EventService {

	public void createEvent(final Event event) {
		if (event.getId() != null) {
			throw new IllegalArgumentException("Not a new event.");
		}
		getJpaTemplate().persist(event);
	}

	public void updateEvent(Event event) {
		JpaTemplate jpaTemplate = getJpaTemplate();
		jpaTemplate.persist(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
	
	}
	
	public void deleteEvent(Event event) {
		if (event == null) {
			throw new IllegalArgumentException("Event must be specified.");
		}
		JpaTemplate jpaTemplate = getJpaTemplate();
		jpaTemplate.remove(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
	}

	public List<Event> getAllEvents() {
		return null; // TODO proper impl
	}

	public List<Event> getAllFutureEvents() {
		return null; // TODO proper impl
	}

	public void updateEvent(Event event, Position position) {
		JpaTemplate jpaTemplate = getJpaTemplate();
		if (position.getId() == -1) {
			position = jpaTemplate.contains(position) ? position : jpaTemplate.merge(position);
		}
		jpaTemplate.persist(jpaTemplate.contains(event) ? event : jpaTemplate.merge(event));
	}

	public void removePositionFromEvent(Event event, Position position) {
		// TODO proper impl
	}

	public List<Event> getAllUnfilledEvents() {
		return null; // TODO proper impl
	}

	public List<Event> getAllFilledEvents() {
		return null; // TODO proper impl
	}

	public List<Event> getEventsForAdministrator(Person administrator) {
		return null; // TODO proper impl
	}

	public List<Event> findAllEvents() {
		// TODO Auto-generated method stub
		return null;
	}

	public Event findEventByEventId(long id) {
		return getJpaTemplate().find(Event.class,id);
	}

	@SuppressWarnings("unchecked")
	public List<Event> findEventsByStartDate(Timestamp startTime) {
		return getJpaTemplate().find("from Event e where e.startTime > ?", startTime);
	}

	public List<Event> findEventsForAdministrator(Person administrator) {
		// TODO Auto-generated method stub
		return null;
	}

}
